//
//  XLTabBar.swift
//  XLVeibo
//
//  Created by macbook on 16/6/25.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit


//声明协议 需要遵守NSObjectProtocol协议
protocol XLTabBarDelagete: NSObjectProtocol{

    //声明协议方法
    func composeBtnClickPushController()

}


class XLTabBar: UITabBar {

    //声明代理
    weak var XLDelegate:XLTabBarDelagete?
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //调用添加UI的方法
        setupUI()
    }
    /**
     如果重写了initWifeFrame 必须实现下面的方法
     代表该类是通过代码创建的 如果其他人使用该类的时候，要使用sb xib形式使用该方法时，会告诉使用者该方法不可用
     */
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //UITabBarBtn的宽度
        let winth = KScreenW / 5
        
        
        //记录标识
        var index:CGFloat = 0
        
        //通过遍历得到子控件
        for varlue in self.subviews{
        
            if varlue.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                //设置宽度
                varlue.frame.size.width = winth
                varlue.frame.origin.x = index * winth
            
                index++
            //把中间位置空出来
                if index == 2 {
                    
                    index++
                }
            
            }
        
        }
        
        //设置中间按钮居中
        composeBtn.center.x = self.frame.size.width * 0.5
        composeBtn.center.y = self.frame.size.height * 0.5
        
    }
    
    
    //设置视图
    func setupUI(){
    
        addSubview(composeBtn)
    
    }
    
    
    
    
    //懒加载中间按钮
    private lazy var composeBtn:UIButton = {
    
        let btn = UIButton()
       //设置背景图片，及图片
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        //添加点击事件
        btn.addTarget(self, action:#selector(XLTabBar.composeBtnClick),forControlEvents: UIControlEvents.TouchUpInside)
        
       return btn
    }()
    
    
    // MARK: - 中间按钮点击方法
    @objc private func composeBtnClick(){
    
    XLDelegate?.composeBtnClickPushController()
    
    }

    
}





























